<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use think\facade\View;

class Config extends Base
{

    public function getOptions(): array
    {
        return [
            'pk'    => 'id',
            'model' => \app\common\model\Config::class,
            'fast'  => [
                'add'       => 'config/add',
                'edit'      => 'config/edit',
                'saveGroup' => '_saveGroup'
            ]
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->addColumns([
                ['id', 'ID', fn(Col $col) => $col->width(80)],
                ['name', '名称'],
                ['title', '标题'],
                ['type', '类型'],
                ['group', '分组'],
                ['create_time', '添加时间', fn(Col $col) => $col->width(140)],
                ['status', '状态', fn(Col $col) => $col->switch('开启|关闭', '1|0')->fixed('right')]
            ])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSearch([
                ['title', '名称/标题筛选', fn(Search $search) => $search->condition('Like')],
                ['create_time', '添加时间', fn(Search $search) => $search->type('date')]
            ])
            ->fetch();

    }

    public function create($id = 1): string
    {
        $map = array('status' => 1, 'group' => $id);
        View::assign('list', $this->model::where($map)->order('sort', 'asc')->select()->toArray());
        View::assign('id', $id);
        return View::fetch();
    }


}
